package com.neuedu.oasystem.view;

import java.util.Date;
import java.util.List;
import java.util.Scanner;

import com.neuedu.oasystem.entity.Notice;
import com.neuedu.oasystem.service.NoticeService;
import com.neuedu.oasystem.service.impl.NoticeServiceImpl;

public class NoticeView {
	private Scanner scanner = null;
	private static NoticeService noticeService;

	public void init() {
		System.out.println("========================================");
		System.out.println("          欢迎使用公告栏信息管理                                 ");
		System.out.println("========================================");
		System.out.println("1.新增公告                                                                                       ");
		System.out.println("2.删除公告                                                                                       ");
		System.out.println("3.修改公告                                                                                       ");
		System.out.println("4.查看公告                                                                                       ");
		System.out.println("5.退出公告栏界面                                                                           ");
		System.out.println("========================================");
	}

	public void work() {

		scanner = new Scanner(System.in);
		noticeService = new NoticeServiceImpl();

		boolean running = true;
		while (running) {
			init();
			System.out.print("请选择：");
			String choose = scanner.nextLine();
			switch (choose) {
			case "1":
				System.out.println("新增公告");
				addNotice();
				break;

			case "2":
				System.out.println("删除公告");
				deleteNotice();
				break;
			case "3":
				System.out.println("修改公告");
				editNotice();
				break;
			case "4":
				System.out.println("查看公告");
				findAllNotice();
				break;
			case "5":
				System.out.println("您选择了退出公告栏界面！");
				running = false;
				break;
			default:
				System.out.println("您的输入有误！请重新输入：");
				break;
			}
		}
	}

	public void editNotice() {
		boolean running1 = true;
		Notice notice = null;
		System.out.println("========================================");
		System.out.println("         欢迎使用修改公告栏信息功能 ！                       ");
		System.out.println("========================================");
		System.out.println("请输入要更改的公告标题：                             ");		
		String noticeTit =null;
		System.out.println("请输入公告栏标题 :              ");
		while(running1) {
		String createTile = scanner.nextLine();
		if(noticeService.findByTitle(createTile)) {
			noticeTit=createTile;
			running1 = false;
		}else {
			System.out.println("该公告标题已存在！请重新输入：");
		}
		}
		
		String noticeTitle = noticeTit;
		System.out.println("请输入公告内容：                                     ");
		String createContent = scanner.nextLine();
		System.out.println("您是否确定修改！");
		outer:while(true) {
		System.out.println("1.确定修改并返回到公告栏信息管理界面                                  ");
		System.out.println("2.取消修改并返回到公告栏信息管理界面                                  ");
		String choose = scanner.nextLine();
		switch (choose) {
		case "1":
			Date now = new Date();
			notice = new Notice(noticeTitle, now, null, 0, createContent);
			boolean bool = noticeService.edit(notice);
			if (bool) {
				System.out.println("修改成功！");
			} else {
				System.out.println("修改失败！");
			}
			System.out.println("三秒后自动返回公告栏信息管理界面！");
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
			}
			break outer;
		case "2":
			System.out.println("你已经取消修改公告！");
			System.out.println("三秒后自动返回公告栏信息管理界面！");
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
			}
			break outer;
		default:
			System.out.println("您的输入有误！请重新输入：");
			break;
			}
		}
	}

	public void addNotice() {
		boolean running1 = true;
		boolean running2 = true;
		boolean running3 = true;
		boolean running4 = true;
		Notice notice = null;
		outer1:while (running1) {
			System.out.println("========================================");
			System.out.println("         欢迎使用新建公告栏信息 ！                               ");
			System.out.println("========================================");
			System.out.println("请输入创建者的ID:          ");
			String createIdstString = null;
			while(running2) {
				String createId=null;
			do {
				try {

					createId = scanner.nextLine();
					if (Integer.valueOf(createId) instanceof Integer) {
						break;
					} else {
						createId = scanner.nextLine();
					}
				} catch (Exception e) {
					System.out.println("您的输入有误，请重新输入：");
				}
			} while (true);
			int createIdInt = Integer.parseInt(createId);
				if(noticeService.findId(createIdInt)) {
					createIdstString=createId;
					running2 = false;
				}else {
					System.out.println("该ID不存在！请重新输入：");
				}
				}
			int createId = Integer.parseInt(createIdstString);
			String noticeTit =null;
			System.out.println("请输入公告栏标题：                              ");
			while(running3) {
			String noticeT1 = scanner.nextLine();
			if(noticeService.findByTitle(noticeT1)) {
				System.out.println("该公告标题已存在！请重新输入：");
			}else {
				noticeTit=noticeT1;
				running3 = false;
			}
			}
			String noticeTitle = noticeTit;
			System.out.println("请输入公告内容:           ");
			String noticeContent = scanner.nextLine();
			while(true) {
			System.out.println("请选择：");
			System.out.println("1.发布并退出                                                                             ");
			System.out.println("2.返回到公告信息管理界面 ！                                                ");
			String choose = scanner.nextLine();
			outer:switch (choose) {
			case "1":
				Date now = new Date();
				notice = new Notice(noticeTitle, now, null, createId, noticeContent);
				boolean bool = noticeService.add(notice);
				noticeService.editById(createId);
				if (bool) {
					System.out.println("发布成功！");
				} else {
					System.out.println("发布失败！");
				}
				System.out.println("三秒后自动返回公告栏信息管理界面！");
				try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
				}
				running1 = false;

				break outer1;
			case "2":
				if (running4) {
					System.out.println("返回到公告信息管理界面,会使得您刚新增的公告信息没发布,您确定返回吗？ ");
					while (true) {
						System.out.println("请选择：");
						System.out.println("1.确定");
						System.out.println("2.取消");
						String choose1 = scanner.nextLine();
						switch (choose1) {
						case "1":
							System.out.println("您已经取消发布公告！");
							System.out.println("三秒后自动返回公告栏信息管理界面！");
							try {
								Thread.sleep(3000);
							} catch (InterruptedException e) {
							}
							break outer1;
						case "2":
							System.out.println("请重新选择是否发布!");
							break outer;
						default:
							System.out.println("您的输入有误！");
							break;
						}
					}
				}
				break;
			default:
				System.out.println("您的输入有误！");
				break;
			}
		}}
	}

	public void deleteNotice() {
		boolean running = true;
		System.out.println("========================================");
		System.out.println("           删除公告栏信息 ！                                          ");
		System.out.println("========================================");
		System.out.println("请输入要删除的公告栏标题:                  ");
		String createTitle = scanner.nextLine();
		outer:while(running) {
		System.out.println("1.确定删除并返回到公告信息管理界面                                     ");
		System.out.println("2.取消删除并返回到公告信息管理界面                                     ");
		String choose = scanner.nextLine();
		switch (choose) {
		case "1":
			noticeService.deleteByTitle(createTitle);
			System.out.println("您已删除成功！");
			System.out.println("三秒后自动返回公告栏信息管理界面！");
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
			}
			break outer;
		case "2":
			System.out.println("您已取消删除！");
			System.out.println("三秒后自动返回公告栏信息管理界面！");
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
			}
			break outer;
		default:
			System.out.println("您的输入有误！请重新输入：");
			break;
			}
		}
	}

	public void findAllNotice() {
		List<Notice> list = noticeService.findAll();
		for (Notice notice : list) {
			System.out.println("公告标题：" + notice.getNoticeTitle() + "\n" + "公告内容：" + notice.getNoticeContent() + "\n"
					+ "发布者名称：" + notice.getEmpName() + "\n" + "发布者ID：" + notice.getEmpId() + "\n" + "发布时间："
					+ notice.getNoticeDate() + "\n");
		}
	}
	
}

